package com.dss.dsc.serlvet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.util.ajax.JSON;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;
import org.eclipse.jetty.websocket.WebSocket.Connection;

import com.dss.dsc.model.ContentsModel;
import com.dss.dsc.server.WebAppServer;

import dsc.model.Array;
import dsc.model.util.GefModelUtil;
import dsc.model.util.JsonConvert;
//import dsc.model.view.Complete;
//import dsc.model.view.Link;
//import dsc.model.view.Sys;
import dsc.model.view.SysForView.Complete.*;
import dsc.model.view.SysForView.Complete;
import dsc.model.view.SysForView;

public class SystemForViewServlet extends WebSocketServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) {
		// TODO Auto-generated method stub
		return new TailorSocket();
	}

	/**
	 * Constructor of the object.
	 */
	public SystemForViewServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// getServletContext().getNamedDispatcher("default").forward(request,
		// response);
		response.getWriter().write("get");

	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().write("post");
		// getServletContext().getNamedDispatcher("default").forward(request,
		// response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		super.init();
	}

	class TailorSocket implements WebSocket.OnTextMessage {

		private Connection _connection;

		public void onClose(int closeCode, String message) {
			// _members.remove(this);
		}

		public void sendMessage(String data) throws IOException {
			_connection.sendMessage(data);
		}

		public void onMessage(String data) {		
			System.out.println(data);
			String path = WebAppServer.readIni("rsc.ini") + "eclipse/" + data;
//			String path = "C:/Documents and Settings/Administrator/桌面/webEditor" + "/eclipse/" + data;
			GefModelUtil mUtil = GefModelUtil.instance();
			Object obj = mUtil.readFile(path);
			Sys sys = new Sys();
			Array<Link> links= new Array<Link>();
			mUtil.convertContentsModel((ContentsModel) obj,sys,links);
			Complete complete = new Complete();
			complete.link = links;
			if(sys.content != null){
				complete.sys = sys;//.content.composite().subSys;
			}
			
			SysForView sysForView = new SysForView();
			sysForView.pick = SysForView.complete_id;
			sysForView.picked = complete;
//			
			String s = JsonConvert.instance().sysForView2Json(sysForView);
			System.out.println(s);
			try {
				this.sendMessage(s);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				_connection.close();
			}
		
		}

		public boolean isOpen() {
			return _connection.isOpen();
		}

		public void onOpen(Connection connection) {
			_connection = connection;
		}
	}
}
